package com.qf.user.service.impl;

import com.qf.user.dao.ProductMapper;
import com.qf.user.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.user.service.impl
 * @Description:
 * @Date 2022/7/30 19:31
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int buyProduct(Integer productId, int sum) {
        int count = productMapper.selectSumById(productId);
        if (count >= sum){
            count -= sum;
        }
        return productMapper.updateSumById(productId, count);
    }
}
